<script setup lang="ts">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch } from 'vue'
import { ViewData } from '@/api/types/userType'
import { useValidator } from '@/hooks/web/useValidator'

const { required} = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<ViewData>>,
    default: () => null
  },
  formSchema: {
    type: Array as PropType<FormSchema[]>,
    default: () => []
  }
})
const { register, methods, elFormRef } = useForm({schema: props.formSchema});
const rules = reactive({
  name: [required()],
})


watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return;
    const { setValues } = methods;
    setValues(currentRow) ;   
    elFormRef;
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})
</script>

<template>
  <Form :rules="rules"  @register="register" >    
      <template #userName v-if="currentRow!=null">
        <span>{{currentRow?.userName}}
        </span>        
      </template>
  </Form>
</template>
